home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1993 July / InfoMagic USENET CD-ROM July 1993.ISO / sources / unix / volume9 / uemacs3.8b / part10 < prev    next >
Encoding:
Internet Message Format  |  1987-03-16  |  44.7 KB

  1. Subject:  v09i042:  MicroEMACS, version 3.8b, Part10/14
  2. Newsgroups: mod.sources
  3. Approved: rs@mirror.TMC.COM
  4.  
  5. Submitted by: ihnp4!itivax!duncan!lawrence (Daniel Lawrence)
  6. Mod.sources: Volume 9, Issue 42
  7. Archive-name: uemacs3.8b/Part10
  8.  
  9. #! /bin/sh
  10. # This is a shell archive.  Remove anything before this line,
  11. # then unpack it by saving it in a file and typing "sh file".
  12. # If this archive is complete, you will see the message:
  13. #        "End of archive 10 (of 14)."
  14. # Contents:  line.c random.c
  15. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  16. echo shar: Extracting \"line.c\" \(18858 characters\)
  17. if test -f line.c ; then 
  18.   echo shar: Will not over-write existing file \"line.c\"
  19. else
  20. sed "s/^X//" >line.c <<'END_OF_line.c'
  21. X/*
  22. X * The functions in this file are a general set of line management utilities.
  23. X * They are the only routines that touch the text. They also touch the buffer
  24. X * and window structures, to make sure that the necessary updating gets done.
  25. X * There are routines in this file that handle the kill buffer too. It isn't
  26. X * here for any good reason.
  27. X *
  28. X * Note that this code only updates the dot and mark values in the window list.
  29. X * Since all the code acts on the current window, the buffer that we are
  30. X * editing must be being displayed, which means that "b_nwnd" is non zero,
  31. X * which means that the dot and mark values in the buffer headers are nonsense.
  32. X */
  33. X
  34. X#include        <stdio.h>
  35. X#include    "estruct.h"
  36. X#include        "edef.h"
  37. X
  38. X#if    MEGAMAX
  39. Xoverlay "line"
  40. X#endif
  41. X
  42. XKILL *ykbuf;    /* ptr to current kill buffer chunk being yanked */
  43. Xint ykboff;    /* offset into that chunk */
  44. X
  45. X/*
  46. X * This routine allocates a block of memory large enough to hold a LINE
  47. X * containing "used" characters. The block is always rounded up a bit. Return
  48. X * a pointer to the new block, or NULL if there isn't any memory left. Print a
  49. X * message in the message line if no space.
  50. X */
  51. XLINE    *
  52. Xlalloc(used)
  53. Xregister int    used;
  54. X{
  55. X        register LINE   *lp;
  56. X        register int    size;
  57. X    char *malloc();
  58. X
  59. X        size = (used+NBLOCK-1) & ~(NBLOCK-1);
  60. X        if (size == 0)                          /* Assume that an empty */
  61. X                size = NBLOCK;                  /* line is for type-in. */
  62. X        if ((lp = (LINE *) malloc(sizeof(LINE)+size)) == NULL) {
  63. X                mlwrite("Cannot allocate %d bytes", size);
  64. X                return (NULL);
  65. X        }
  66. X        lp->l_size = size;
  67. X        lp->l_used = used;
  68. X        return (lp);
  69. X}
  70. X
  71. X/*
  72. X * Delete line "lp". Fix all of the links that might point at it (they are
  73. X * moved to offset 0 of the next line. Unlink the line from whatever buffer it
  74. X * might be in. Release the memory. The buffers are updated too; the magic
  75. X * conditions described in the above comments don't hold here.
  76. X */
  77. Xlfree(lp)
  78. Xregister LINE   *lp;
  79. X{
  80. X        register BUFFER *bp;
  81. X        register WINDOW *wp;
  82. X
  83. X        wp = wheadp;
  84. X        while (wp != NULL) {
  85. X                if (wp->w_linep == lp)
  86. X                        wp->w_linep = lp->l_fp;
  87. X                if (wp->w_dotp  == lp) {
  88. X                        wp->w_dotp  = lp->l_fp;
  89. X                        wp->w_doto  = 0;
  90. X                }
  91. X                if (wp->w_markp == lp) {
  92. X                        wp->w_markp = lp->l_fp;
  93. X                        wp->w_marko = 0;
  94. X                }
  95. X                wp = wp->w_wndp;
  96. X        }
  97. X        bp = bheadp;
  98. X        while (bp != NULL) {
  99. X                if (bp->b_nwnd == 0) {
  100. X                        if (bp->b_dotp  == lp) {
  101. X                                bp->b_dotp = lp->l_fp;
  102. X                                bp->b_doto = 0;
  103. X                        }
  104. X                        if (bp->b_markp == lp) {
  105. X                                bp->b_markp = lp->l_fp;
  106. X                                bp->b_marko = 0;
  107. X                        }
  108. X                }
  109. X                bp = bp->b_bufp;
  110. X        }
  111. X        lp->l_bp->l_fp = lp->l_fp;
  112. X        lp->l_fp->l_bp = lp->l_bp;
  113. X        free((char *) lp);
  114. X}
  115. X
  116. X/*
  117. X * This routine gets called when a character is changed in place in the current
  118. X * buffer. It updates all of the required flags in the buffer and window
  119. X * system. The flag used is passed as an argument; if the buffer is being
  120. X * displayed in more than 1 window we change EDIT t HARD. Set MODE if the
  121. X * mode line needs to be updated (the "*" has to be set).
  122. X */
  123. Xlchange(flag)
  124. Xregister int    flag;
  125. X{
  126. X        register WINDOW *wp;
  127. X
  128. X        if (curbp->b_nwnd != 1)                 /* Ensure hard.         */
  129. X                flag = WFHARD;
  130. X        if ((curbp->b_flag&BFCHG) == 0) {       /* First change, so     */
  131. X                flag |= WFMODE;                 /* update mode lines.   */
  132. X                curbp->b_flag |= BFCHG;
  133. X        }
  134. X        wp = wheadp;
  135. X        while (wp != NULL) {
  136. X                if (wp->w_bufp == curbp)
  137. X                        wp->w_flag |= flag;
  138. X                wp = wp->w_wndp;
  139. X        }
  140. X}
  141. X
  142. Xinsspace(f, n)    /* insert spaces forward into text */
  143. X
  144. Xint f, n;    /* default flag and numeric argument */
  145. X
  146. X{
  147. X    linsert(n, ' ');
  148. X    backchar(f, n);
  149. X}
  150. X
  151. X/*
  152. X * Insert "n" copies of the character "c" at the current location of dot. In
  153. X * the easy case all that happens is the text is stored in the line. In the
  154. X * hard case, the line has to be reallocated. When the window list is updated,
  155. X * take special care; I screwed it up once. You always update dot in the
  156. X * current window. You update mark, and a dot in another window, if it is
  157. X * greater than the place where you did the insert. Return TRUE if all is
  158. X * well, and FALSE on errors.
  159. X */
  160. Xlinsert(n, c)
  161. X{
  162. X        register char   *cp1;
  163. X        register char   *cp2;
  164. X        register LINE   *lp1;
  165. X        register LINE   *lp2;
  166. X        register LINE   *lp3;
  167. X        register int    doto;
  168. X        register int    i;
  169. X        register WINDOW *wp;
  170. X
  171. X    if (curbp->b_mode&MDVIEW)    /* don't allow this command if    */
  172. X        return(rdonly());    /* we are in read only mode    */
  173. X        lchange(WFEDIT);
  174. X        lp1 = curwp->w_dotp;                    /* Current line         */
  175. X        if (lp1 == curbp->b_linep) {            /* At the end: special  */
  176. X                if (curwp->w_doto != 0) {
  177. X                        mlwrite("bug: linsert");
  178. X                        return (FALSE);
  179. X                }
  180. X                if ((lp2=lalloc(n)) == NULL)    /* Allocate new line    */
  181. X                        return (FALSE);
  182. X                lp3 = lp1->l_bp;                /* Previous line        */
  183. X                lp3->l_fp = lp2;                /* Link in              */
  184. X                lp2->l_fp = lp1;
  185. X                lp1->l_bp = lp2;
  186. X                lp2->l_bp = lp3;
  187. X                for (i=0; i<n; ++i)
  188. X                        lp2->l_text[i] = c;
  189. X                curwp->w_dotp = lp2;
  190. X                curwp->w_doto = n;
  191. X                return (TRUE);
  192. X        }
  193. X        doto = curwp->w_doto;                   /* Save for later.      */
  194. X        if (lp1->l_used+n > lp1->l_size) {      /* Hard: reallocate     */
  195. X                if ((lp2=lalloc(lp1->l_used+n)) == NULL)
  196. X                        return (FALSE);
  197. X                cp1 = &lp1->l_text[0];
  198. X                cp2 = &lp2->l_text[0];
  199. X                while (cp1 != &lp1->l_text[doto])
  200. X                        *cp2++ = *cp1++;
  201. X                cp2 += n;
  202. X                while (cp1 != &lp1->l_text[lp1->l_used])
  203. X                        *cp2++ = *cp1++;
  204. X                lp1->l_bp->l_fp = lp2;
  205. X                lp2->l_fp = lp1->l_fp;
  206. X                lp1->l_fp->l_bp = lp2;
  207. X                lp2->l_bp = lp1->l_bp;
  208. X                free((char *) lp1);
  209. X        } else {                                /* Easy: in place       */
  210. X                lp2 = lp1;                      /* Pretend new line     */
  211. X                lp2->l_used += n;
  212. X                cp2 = &lp1->l_text[lp1->l_used];
  213. X                cp1 = cp2-n;
  214. X                while (cp1 != &lp1->l_text[doto])
  215. X                        *--cp2 = *--cp1;
  216. X        }
  217. X        for (i=0; i<n; ++i)                     /* Add the characters   */
  218. X                lp2->l_text[doto+i] = c;
  219. X        wp = wheadp;                            /* Update windows       */
  220. X        while (wp != NULL) {
  221. X                if (wp->w_linep == lp1)
  222. X                        wp->w_linep = lp2;
  223. X                if (wp->w_dotp == lp1) {
  224. X                        wp->w_dotp = lp2;
  225. X                        if (wp==curwp || wp->w_doto>doto)
  226. X                                wp->w_doto += n;
  227. X                }
  228. X                if (wp->w_markp == lp1) {
  229. X                        wp->w_markp = lp2;
  230. X                        if (wp->w_marko > doto)
  231. X                                wp->w_marko += n;
  232. X                }
  233. X                wp = wp->w_wndp;
  234. X        }
  235. X        return (TRUE);
  236. X}
  237. X
  238. X/*
  239. X * Insert a newline into the buffer at the current location of dot in the
  240. X * current window. The funny ass-backwards way it does things is not a botch;
  241. X * it just makes the last line in the file not a special case. Return TRUE if
  242. X * everything works out and FALSE on error (memory allocation failure). The
  243. X * update of dot and mark is a bit easier then in the above case, because the
  244. X * split forces more updating.
  245. X */
  246. Xlnewline()
  247. X{
  248. X        register char   *cp1;
  249. X        register char   *cp2;
  250. X        register LINE   *lp1;
  251. X        register LINE   *lp2;
  252. X        register int    doto;
  253. X        register WINDOW *wp;
  254. X
  255. X    if (curbp->b_mode&MDVIEW)    /* don't allow this command if    */
  256. X        return(rdonly());    /* we are in read only mode    */
  257. X        lchange(WFHARD);
  258. X        lp1  = curwp->w_dotp;                   /* Get the address and  */
  259. X        doto = curwp->w_doto;                   /* offset of "."        */
  260. X        if ((lp2=lalloc(doto)) == NULL)         /* New first half line  */
  261. X                return (FALSE);
  262. X        cp1 = &lp1->l_text[0];                  /* Shuffle text around  */
  263. X        cp2 = &lp2->l_text[0];
  264. X        while (cp1 != &lp1->l_text[doto])
  265. X                *cp2++ = *cp1++;
  266. X        cp2 = &lp1->l_text[0];
  267. X        while (cp1 != &lp1->l_text[lp1->l_used])
  268. X                *cp2++ = *cp1++;
  269. X        lp1->l_used -= doto;
  270. X        lp2->l_bp = lp1->l_bp;
  271. X        lp1->l_bp = lp2;
  272. X        lp2->l_bp->l_fp = lp2;
  273. X        lp2->l_fp = lp1;
  274. X        wp = wheadp;                            /* Windows              */
  275. X        while (wp != NULL) {
  276. X                if (wp->w_linep == lp1)
  277. X                        wp->w_linep = lp2;
  278. X                if (wp->w_dotp == lp1) {
  279. X                        if (wp->w_doto < doto)
  280. X                                wp->w_dotp = lp2;
  281. X                        else
  282. X                                wp->w_doto -= doto;
  283. X                }
  284. X                if (wp->w_markp == lp1) {
  285. X                        if (wp->w_marko < doto)
  286. X                                wp->w_markp = lp2;
  287. X                        else
  288. X                                wp->w_marko -= doto;
  289. X                }
  290. X                wp = wp->w_wndp;
  291. X        }
  292. X        return (TRUE);
  293. X}
  294. X
  295. X/*
  296. X * This function deletes "n" bytes, starting at dot. It understands how do deal
  297. X * with end of lines, etc. It returns TRUE if all of the characters were
  298. X * deleted, and FALSE if they were not (because dot ran into the end of the
  299. X * buffer. The "kflag" is TRUE if the text should be put in the kill buffer.
  300. X */
  301. Xldelete(n, kflag)
  302. X
  303. Xlong n;        /* # of chars to delete */
  304. Xint kflag;    /* put killed text in kill buffer flag */
  305. X
  306. X{
  307. X        register char   *cp1;
  308. X        register char   *cp2;
  309. X        register LINE   *dotp;
  310. X        register int    doto;
  311. X        register int    chunk;
  312. X        register WINDOW *wp;
  313. X
  314. X    if (curbp->b_mode&MDVIEW)    /* don't allow this command if    */
  315. X        return(rdonly());    /* we are in read only mode    */
  316. X        while (n != 0) {
  317. X                dotp = curwp->w_dotp;
  318. X                doto = curwp->w_doto;
  319. X                if (dotp == curbp->b_linep)     /* Hit end of buffer.   */
  320. X                        return (FALSE);
  321. X                chunk = dotp->l_used-doto;      /* Size of chunk.       */
  322. X                if (chunk > n)
  323. X                        chunk = n;
  324. X                if (chunk == 0) {               /* End of line, merge.  */
  325. X                        lchange(WFHARD);
  326. X                        if (ldelnewline() == FALSE
  327. X                        || (kflag!=FALSE && kinsert('\n')==FALSE))
  328. X                                return (FALSE);
  329. X                        --n;
  330. X                        continue;
  331. X                }
  332. X                lchange(WFEDIT);
  333. X                cp1 = &dotp->l_text[doto];      /* Scrunch text.        */
  334. X                cp2 = cp1 + chunk;
  335. X                if (kflag != FALSE) {           /* Kill?                */
  336. X                        while (cp1 != cp2) {
  337. X                                if (kinsert(*cp1) == FALSE)
  338. X                                        return (FALSE);
  339. X                                ++cp1;
  340. X                        }
  341. X                        cp1 = &dotp->l_text[doto];
  342. X                }
  343. X                while (cp2 != &dotp->l_text[dotp->l_used])
  344. X                        *cp1++ = *cp2++;
  345. X                dotp->l_used -= chunk;
  346. X                wp = wheadp;                    /* Fix windows          */
  347. X                while (wp != NULL) {
  348. X                        if (wp->w_dotp==dotp && wp->w_doto>=doto) {
  349. X                                wp->w_doto -= chunk;
  350. X                                if (wp->w_doto < doto)
  351. X                                        wp->w_doto = doto;
  352. X                        }
  353. X                        if (wp->w_markp==dotp && wp->w_marko>=doto) {
  354. X                                wp->w_marko -= chunk;
  355. X                                if (wp->w_marko < doto)
  356. X                                        wp->w_marko = doto;
  357. X                        }
  358. X                        wp = wp->w_wndp;
  359. X                }
  360. X                n -= chunk;
  361. X        }
  362. X        return (TRUE);
  363. X}
  364. X
  365. X/*
  366. X * Delete a newline. Join the current line with the next line. If the next line
  367. X * is the magic header line always return TRUE; merging the last line with the
  368. X * header line can be thought of as always being a successful operation, even
  369. X * if nothing is done, and this makes the kill buffer work "right". Easy cases
  370. X * can be done by shuffling data around. Hard cases require that lines be moved
  371. X * about in memory. Return FALSE on error and TRUE if all looks ok. Called by
  372. X * "ldelete" only.
  373. X */
  374. Xldelnewline()
  375. X{
  376. X        register char   *cp1;
  377. X        register char   *cp2;
  378. X        register LINE   *lp1;
  379. X        register LINE   *lp2;
  380. X        register LINE   *lp3;
  381. X        register WINDOW *wp;
  382. X
  383. X    if (curbp->b_mode&MDVIEW)    /* don't allow this command if    */
  384. X        return(rdonly());    /* we are in read only mode    */
  385. X        lp1 = curwp->w_dotp;
  386. X        lp2 = lp1->l_fp;
  387. X        if (lp2 == curbp->b_linep) {            /* At the buffer end.   */
  388. X                if (lp1->l_used == 0)           /* Blank line.          */
  389. X                        lfree(lp1);
  390. X                return (TRUE);
  391. X        }
  392. X        if (lp2->l_used <= lp1->l_size-lp1->l_used) {
  393. X                cp1 = &lp1->l_text[lp1->l_used];
  394. X                cp2 = &lp2->l_text[0];
  395. X                while (cp2 != &lp2->l_text[lp2->l_used])
  396. X                        *cp1++ = *cp2++;
  397. X                wp = wheadp;
  398. X                while (wp != NULL) {
  399. X                        if (wp->w_linep == lp2)
  400. X                                wp->w_linep = lp1;
  401. X                        if (wp->w_dotp == lp2) {
  402. X                                wp->w_dotp  = lp1;
  403. X                                wp->w_doto += lp1->l_used;
  404. X                        }
  405. X                        if (wp->w_markp == lp2) {
  406. X                                wp->w_markp  = lp1;
  407. X                                wp->w_marko += lp1->l_used;
  408. X                        }
  409. X                        wp = wp->w_wndp;
  410. X                }
  411. X                lp1->l_used += lp2->l_used;
  412. X                lp1->l_fp = lp2->l_fp;
  413. X                lp2->l_fp->l_bp = lp1;
  414. X                free((char *) lp2);
  415. X                return (TRUE);
  416. X        }
  417. X        if ((lp3=lalloc(lp1->l_used+lp2->l_used)) == NULL)
  418. X                return (FALSE);
  419. X        cp1 = &lp1->l_text[0];
  420. X        cp2 = &lp3->l_text[0];
  421. X        while (cp1 != &lp1->l_text[lp1->l_used])
  422. X                *cp2++ = *cp1++;
  423. X        cp1 = &lp2->l_text[0];
  424. X        while (cp1 != &lp2->l_text[lp2->l_used])
  425. X                *cp2++ = *cp1++;
  426. X        lp1->l_bp->l_fp = lp3;
  427. X        lp3->l_fp = lp2->l_fp;
  428. X        lp2->l_fp->l_bp = lp3;
  429. X        lp3->l_bp = lp1->l_bp;
  430. X        wp = wheadp;
  431. X        while (wp != NULL) {
  432. X                if (wp->w_linep==lp1 || wp->w_linep==lp2)
  433. X                        wp->w_linep = lp3;
  434. X                if (wp->w_dotp == lp1)
  435. X                        wp->w_dotp  = lp3;
  436. X                else if (wp->w_dotp == lp2) {
  437. X                        wp->w_dotp  = lp3;
  438. X                        wp->w_doto += lp1->l_used;
  439. X                }
  440. X                if (wp->w_markp == lp1)
  441. X                        wp->w_markp  = lp3;
  442. X                else if (wp->w_markp == lp2) {
  443. X                        wp->w_markp  = lp3;
  444. X                        wp->w_marko += lp1->l_used;
  445. X                }
  446. X                wp = wp->w_wndp;
  447. X        }
  448. X        free((char *) lp1);
  449. X        free((char *) lp2);
  450. X        return (TRUE);
  451. X}
  452. X
  453. X/*
  454. X * Delete all of the text saved in the kill buffer. Called by commands when a
  455. X * new kill context is being created. The kill buffer array is released, just
  456. X * in case the buffer has grown to immense size. No errors.
  457. X */
  458. Xkdelete()
  459. X{
  460. X    KILL *kp;    /* ptr to scan kill buffer chunk list */
  461. X
  462. X        if (kbufh != NULL) {
  463. X
  464. X        /* first, delete all the chunks */
  465. X            kbufp = kbufh;
  466. X            while (kbufp != NULL) {
  467. X                kp = kbufp->d_next;
  468. X                free(kbufp);
  469. X                kbufp = kp;
  470. X            }
  471. X
  472. X        /* and reset all the kill buffer pointers */
  473. X        kbufh = kbufp = NULL;
  474. X        kused = KBLOCK;                
  475. X        }
  476. X}
  477. X
  478. X/*
  479. X * Insert a character to the kill buffer, allocating new chunks as needed.
  480. X * Return TRUE if all is well, and FALSE on errors.
  481. X */
  482. X
  483. Xkinsert(c)
  484. X
  485. Xint c;        /* character to insert in the kill buffer */
  486. X
  487. X{
  488. X    KILL *nchunk;    /* ptr to newly malloced chunk */
  489. X
  490. X    /* check to see if we need a new chunk */
  491. X    if (kused >= KBLOCK) {
  492. X        if ((nchunk = (KILL *)malloc(sizeof(KILL))) == NULL)
  493. X            return(FALSE);
  494. X        if (kbufh == NULL)    /* set head ptr if first time */
  495. X            kbufh = nchunk;
  496. X        if (kbufp != NULL)    /* point the current to this new one */
  497. X            kbufp->d_next = nchunk;
  498. X        kbufp = nchunk;
  499. X        kbufp->d_next = NULL;
  500. X        kused = 0;
  501. X    }
  502. X
  503. X    /* and now insert the character */
  504. X    kbufp->d_chunk[kused++] = c;
  505. X    return(TRUE);
  506. X}
  507. X
  508. X/*
  509. X * Yank text back from the kill buffer. This is really easy. All of the work
  510. X * is done by the standard insert routines. All you do is run the loop, and
  511. X * check for errors. Bound to "C-Y".
  512. X */
  513. Xyank(f, n)
  514. X{
  515. X        register int    c;
  516. X        register int    i;
  517. X    register char    *sp;    /* pointer into string to insert */
  518. X    KILL *kp;        /* pointer into kill buffer */
  519. X
  520. X    if (curbp->b_mode&MDVIEW)    /* don't allow this command if    */
  521. X        return(rdonly());    /* we are in read only mode    */
  522. X        if (n < 0)
  523. X                return (FALSE);
  524. X    /* make sure there is something to yank */
  525. X    if (kbufh == NULL)
  526. X        return(TRUE);        /* not an error, just nothing */
  527. X
  528. X    /* for each time.... */
  529. X        while (n--) {
  530. X        kp = kbufh;
  531. X        while (kp != NULL) {
  532. X            if (kp->d_next == NULL)
  533. X                i = kused;
  534. X            else
  535. X                i = KBLOCK;
  536. X            sp = kp->d_chunk;
  537. X            while (i--) {
  538. X                            if ((c = *sp++) == '\n') {
  539. X                                    if (lnewline(FALSE, 1) == FALSE)
  540. X                                            return (FALSE);
  541. X                            } else {
  542. X                                    if (linsert(1, c) == FALSE)
  543. X                                            return (FALSE);
  544. X                            }
  545. X                    }
  546. X                    kp = kp->d_next;
  547. X                }
  548. X        }
  549. X        return (TRUE);
  550. X}
  551. X
  552. X
  553. END_OF_line.c
  554. if test 18858 -ne `wc -c <line.c`; then
  555.     echo shar: \"line.c\" unpacked with wrong size!
  556. fi
  557. # end of overwriting check
  558. fi
  559. echo shar: Extracting \"random.c\" \(23797 characters\)
  560. if test -f random.c ; then 
  561.   echo shar: Will not over-write existing file \"random.c\"
  562. else
  563. sed "s/^X//" >random.c <<'END_OF_random.c'
  564. X/*
  565. X * This file contains the command processing functions for a number of random
  566. X * commands. There is no functional grouping here, for sure.
  567. X */
  568. X
  569. X#include        <stdio.h>
  570. X#include    "estruct.h"
  571. X#include        "edef.h"
  572. X
  573. X#if    MEGAMAX & ST520
  574. Xoverlay "random"
  575. X
  576. Xextern int STncolors;
  577. X#endif
  578. X
  579. Xint     tabsize;                        /* Tab size (0: use real tabs)  */
  580. X
  581. X/*
  582. X * Set fill column to n.
  583. X */
  584. Xsetfillcol(f, n)
  585. X{
  586. X        fillcol = n;
  587. X    mlwrite("[Fill column is %d]",n);
  588. X        return(TRUE);
  589. X}
  590. X
  591. X/*
  592. X * Display the current position of the cursor, in origin 1 X-Y coordinates,
  593. X * the character that is under the cursor (in hex), and the fraction of the
  594. X * text that is before the cursor. The displayed column is not the current
  595. X * column, but the column that would be used on an infinite width display.
  596. X * Normally this is bound to "C-X =".
  597. X */
  598. Xshowcpos(f, n)
  599. X{
  600. X        register LINE   *lp;        /* current line */
  601. X        register long   numchars;    /* # of chars in file */
  602. X        register int    numlines;    /* # of lines in file */
  603. X        register long   predchars;    /* # chars preceding point */
  604. X        register int    predlines;    /* # lines preceding point */
  605. X        register int    curchar;    /* character under cursor */
  606. X        int ratio;
  607. X        int col;
  608. X    int savepos;            /* temp save for current offset */
  609. X    int ecol;            /* column pos/end of current line */
  610. X
  611. X    /* starting at the beginning of the buffer */
  612. X        lp = lforw(curbp->b_linep);
  613. X
  614. X    /* start counting chars and lines */
  615. X        numchars = 0;
  616. X        numlines = 0;
  617. X        while (lp != curbp->b_linep) {
  618. X        /* if we are on the current line, record it */
  619. X        if (lp == curwp->w_dotp) {
  620. X            predlines = numlines;
  621. X            predchars = numchars + curwp->w_doto;
  622. X            if ((curwp->w_doto) == llength(lp))
  623. X                curchar = '\n';
  624. X            else
  625. X                curchar = lgetc(lp, curwp->w_doto);
  626. X        }
  627. X        /* on to the next line */
  628. X        ++numlines;
  629. X        numchars += llength(lp) + 1;
  630. X        lp = lforw(lp);
  631. X        }
  632. X
  633. X    /* if at end of file, record it */
  634. X    if (curwp->w_dotp == curbp->b_linep) {
  635. X        predlines = numlines;
  636. X        predchars = numchars;
  637. X    }
  638. X
  639. X    /* Get real column and end-of-line column. */
  640. X    col = getccol(FALSE);
  641. X    savepos = curwp->w_doto;
  642. X    curwp->w_doto = llength(curwp->w_dotp);
  643. X    ecol = getccol(FALSE);
  644. X    curwp->w_doto = savepos;
  645. X
  646. X        ratio = 0;              /* Ratio before dot. */
  647. X        if (numchars != 0)
  648. X                ratio = (100L*predchars) / numchars;
  649. X
  650. X    /* summarize and report the info */
  651. X    mlwrite("Line %d/%d Col %d/%d Char %D/%D (%d%%) char = 0x%x",
  652. X        predlines+1, numlines+1, col, ecol,
  653. X        predchars, numchars, ratio, curchar);
  654. X        return (TRUE);
  655. X}
  656. X
  657. Xgetcline()    /* get the current line number */
  658. X
  659. X{
  660. X        register LINE   *lp;        /* current line */
  661. X        register int    numlines;    /* # of lines before point */
  662. X
  663. X    /* starting at the beginning of the buffer */
  664. X        lp = lforw(curbp->b_linep);
  665. X
  666. X    /* start counting lines */
  667. X        numlines = 0;
  668. X        while (lp != curbp->b_linep) {
  669. X        /* if we are on the current line, record it */
  670. X        if (lp == curwp->w_dotp)
  671. X            break;
  672. X        ++numlines;
  673. X        lp = lforw(lp);
  674. X        }
  675. X
  676. X    /* and return the resulting count */
  677. X    return(numlines + 1);
  678. X}
  679. X
  680. X/*
  681. X * Return current column.  Stop at first non-blank given TRUE argument.
  682. X */
  683. Xgetccol(bflg)
  684. Xint bflg;
  685. X{
  686. X        register int c, i, col;
  687. X        col = 0;
  688. X        for (i=0; i<curwp->w_doto; ++i) {
  689. X                c = lgetc(curwp->w_dotp, i);
  690. X                if (c!=' ' && c!='\t' && bflg)
  691. X                        break;
  692. X                if (c == '\t')
  693. X                        col |= 0x07;
  694. X                else if (c<0x20 || c==0x7F)
  695. X                        ++col;
  696. X                ++col;
  697. X        }
  698. X        return(col);
  699. X}
  700. X
  701. X/*
  702. X * Set current column.
  703. X */
  704. Xsetccol(pos)
  705. X
  706. Xint pos;    /* position to set cursor */
  707. X
  708. X{
  709. X        register int c;        /* character being scanned */
  710. X    register int i;        /* index into current line */
  711. X    register int col;    /* current cursor column   */
  712. X    register int llen;    /* length of line in bytes */
  713. X
  714. X    col = 0;
  715. X    llen = llength(curwp->w_dotp);
  716. X
  717. X    /* scan the line until we are at or past the target column */
  718. X    for (i = 0; i < llen; ++i) {
  719. X        /* upon reaching the target, drop out */
  720. X        if (col >= pos)
  721. X            break;
  722. X
  723. X        /* advance one character */
  724. X                c = lgetc(curwp->w_dotp, i);
  725. X                if (c == '\t')
  726. X                        col |= 0x07;
  727. X                else if (c<0x20 || c==0x7F)
  728. X                        ++col;
  729. X                ++col;
  730. X        }
  731. X    /* if not long enough... */
  732. X    if (col < pos)
  733. X        return(FALSE);
  734. X
  735. X    /* otherwise...set us at the new position */
  736. X    curwp->w_doto = i;
  737. X    return(TRUE);
  738. X}
  739. X
  740. X/*
  741. X * Twiddle the two characters on either side of dot. If dot is at the end of
  742. X * the line twiddle the two characters before it. Return with an error if dot
  743. X * is at the beginning of line; it seems to be a bit pointless to make this
  744. X * work. This fixes up a very common typo with a single stroke. Normally bound
  745. X * to "C-T". This always works within a line, so "WFEDIT" is good enough.
  746. X */
  747. Xtwiddle(f, n)
  748. X{
  749. X        register LINE   *dotp;
  750. X        register int    doto;
  751. X        register int    cl;
  752. X        register int    cr;
  753. X
  754. X    if (curbp->b_mode&MDVIEW)    /* don't allow this command if    */
  755. X        return(rdonly());    /* we are in read only mode    */
  756. X        dotp = curwp->w_dotp;
  757. X        doto = curwp->w_doto;
  758. X        if (doto==llength(dotp) && --doto<0)
  759. X                return (FALSE);
  760. X        cr = lgetc(dotp, doto);
  761. X        if (--doto < 0)
  762. X                return (FALSE);
  763. X        cl = lgetc(dotp, doto);
  764. X        lputc(dotp, doto+0, cr);
  765. X        lputc(dotp, doto+1, cl);
  766. X        lchange(WFEDIT);
  767. X        return (TRUE);
  768. X}
  769. X
  770. X/*
  771. X * Quote the next character, and insert it into the buffer. All the characters
  772. X * are taken literally, with the exception of the newline, which always has
  773. X * its line splitting meaning. The character is always read, even if it is
  774. X * inserted 0 times, for regularity. Bound to "C-Q"
  775. X */
  776. Xquote(f, n)
  777. X{
  778. X        register int    s;
  779. X        register int    c;
  780. X
  781. X    if (curbp->b_mode&MDVIEW)    /* don't allow this command if    */
  782. X        return(rdonly());    /* we are in read only mode    */
  783. X        c = tgetc();
  784. X        if (n < 0)
  785. X                return (FALSE);
  786. X        if (n == 0)
  787. X                return (TRUE);
  788. X        if (c == '\n') {
  789. X                do {
  790. X                        s = lnewline();
  791. X                } while (s==TRUE && --n);
  792. X                return (s);
  793. X        }
  794. X        return (linsert(n, c));
  795. X}
  796. X
  797. X/*
  798. X * Set tab size if given non-default argument (n <> 1).  Otherwise, insert a
  799. X * tab into file.  If given argument, n, of zero, change to true tabs.
  800. X * If n > 1, simulate tab stop every n-characters using spaces. This has to be
  801. X * done in this slightly funny way because the tab (in ASCII) has been turned
  802. X * into "C-I" (in 10 bit code) already. Bound to "C-I".
  803. X */
  804. Xtab(f, n)
  805. X{
  806. X        if (n < 0)
  807. X                return (FALSE);
  808. X        if (n == 0 || n > 1) {
  809. X                tabsize = n;
  810. X                return(TRUE);
  811. X        }
  812. X        if (! tabsize)
  813. X                return(linsert(1, '\t'));
  814. X        return(linsert(tabsize - (getccol(FALSE) % tabsize), ' '));
  815. X}
  816. X
  817. X/*
  818. X * Open up some blank space. The basic plan is to insert a bunch of newlines,
  819. X * and then back up over them. Everything is done by the subcommand
  820. X * procerssors. They even handle the looping. Normally this is bound to "C-O".
  821. X */
  822. Xopenline(f, n)
  823. X{
  824. X        register int    i;
  825. X        register int    s;
  826. X
  827. X    if (curbp->b_mode&MDVIEW)    /* don't allow this command if    */
  828. X        return(rdonly());    /* we are in read only mode    */
  829. X        if (n < 0)
  830. X                return (FALSE);
  831. X        if (n == 0)
  832. X                return (TRUE);
  833. X        i = n;                                  /* Insert newlines.     */
  834. X        do {
  835. X                s = lnewline();
  836. X        } while (s==TRUE && --i);
  837. X        if (s == TRUE)                          /* Then back up overtop */
  838. X                s = backchar(f, n);             /* of them all.         */
  839. X        return (s);
  840. X}
  841. X
  842. X/*
  843. X * Insert a newline. Bound to "C-M". If we are in CMODE, do automatic
  844. X * indentation as specified.
  845. X */
  846. Xnewline(f, n)
  847. X{
  848. X    register int    s;
  849. X
  850. X    if (curbp->b_mode&MDVIEW)    /* don't allow this command if    */
  851. X        return(rdonly());    /* we are in read only mode    */
  852. X    if (n < 0)
  853. X        return (FALSE);
  854. X
  855. X    /* if we are in C mode and this is a default <NL> */
  856. X    if (n == 1 && (curbp->b_mode & MDCMOD) &&
  857. X        curwp->w_dotp != curbp->b_linep)
  858. X        return(cinsert());
  859. X
  860. X        /*
  861. X         * If a newline was typed, fill column is defined, the argument is non-
  862. X         * negative, wrap mode is enabled, and we are now past fill column,
  863. X     * and we are not read-only, perform word wrap.
  864. X         */
  865. X        if ((curwp->w_bufp->b_mode & MDWRAP) && fillcol > 0 &&
  866. X        getccol(FALSE) > fillcol &&
  867. X        (curwp->w_bufp->b_mode & MDVIEW) == FALSE)
  868. X        execute(META|SPEC|'W', FALSE, 1);
  869. X
  870. X    /* insert some lines */
  871. X    while (n--) {
  872. X        if ((s=lnewline()) != TRUE)
  873. X            return (s);
  874. X    }
  875. X    return (TRUE);
  876. X}
  877. X
  878. Xcinsert()    /* insert a newline and indentation for C */
  879. X
  880. X{
  881. X    register char *cptr;    /* string pointer into text to copy */
  882. X    register int tptr;    /* index to scan into line */
  883. X    register int bracef;    /* was there a brace at the end of line? */
  884. X    register int i;
  885. X    char ichar[NSTRING];    /* buffer to hold indent of last line */
  886. X
  887. X    /* grab a pointer to text to copy indentation from */
  888. X    cptr = &curwp->w_dotp->l_text[0];
  889. X
  890. X    /* check for a brace */
  891. X    tptr = curwp->w_doto - 1;
  892. X    bracef = (cptr[tptr] == '{');
  893. X
  894. X    /* save the indent of the previous line */
  895. X    i = 0;
  896. X    while ((i < tptr) && (cptr[i] == ' ' || cptr[i] == '\t')
  897. X        && (i < NSTRING - 1)) {
  898. X        ichar[i] = cptr[i];
  899. X        ++i;
  900. X    }
  901. X    ichar[i] = 0;        /* terminate it */
  902. X
  903. X    /* put in the newline */
  904. X    if (lnewline() == FALSE)
  905. X        return(FALSE);
  906. X
  907. X    /* and the saved indentation */
  908. X    i = 0;
  909. X    while (ichar[i])
  910. X        linsert(1, ichar[i++]);
  911. X
  912. X    /* and one more tab for a brace */
  913. X    if (bracef)
  914. X        tab(FALSE, 1);
  915. X
  916. X    return(TRUE);
  917. X}
  918. X
  919. Xinsbrace(n, c)    /* insert a brace into the text here...we are in CMODE */
  920. X
  921. Xint n;    /* repeat count */
  922. Xint c;    /* brace to insert (always { for now) */
  923. X
  924. X{
  925. X    register int ch;    /* last character before input */
  926. X    register int i;
  927. X    register int target;    /* column brace should go after */
  928. X
  929. X    /* if we are at the beginning of the line, no go */
  930. X    if (curwp->w_doto == 0)
  931. X        return(linsert(n,c));
  932. X        
  933. X    /* scan to see if all space before this is white space */
  934. X    for (i = curwp->w_doto - 1; i >= 0; --i) {
  935. X        ch = lgetc(curwp->w_dotp, i);
  936. X        if (ch != ' ' && ch != '\t')
  937. X            return(linsert(n, c));
  938. X    }
  939. X
  940. X    /* delete back first */
  941. X    target = getccol(FALSE);    /* calc where we will delete to */
  942. X    target -= 1;
  943. X    target -= target % (tabsize == 0 ? 8 : tabsize);
  944. X    while (getccol(FALSE) > target)
  945. X        backdel(FALSE, 1);
  946. X
  947. X    /* and insert the required brace(s) */
  948. X    return(linsert(n, c));
  949. X}
  950. X
  951. Xinspound()    /* insert a # into the text here...we are in CMODE */
  952. X
  953. X{
  954. X    register int ch;    /* last character before input */
  955. X    register int i;
  956. X
  957. X    /* if we are at the beginning of the line, no go */
  958. X    if (curwp->w_doto == 0)
  959. X        return(linsert(1,'#'));
  960. X        
  961. X    /* scan to see if all space before this is white space */
  962. X    for (i = curwp->w_doto - 1; i >= 0; --i) {
  963. X        ch = lgetc(curwp->w_dotp, i);
  964. X        if (ch != ' ' && ch != '\t')
  965. X            return(linsert(1, '#'));
  966. X    }
  967. X
  968. X    /* delete back first */
  969. X    while (getccol(FALSE) >= 1)
  970. X        backdel(FALSE, 1);
  971. X
  972. X    /* and insert the required pound */
  973. X    return(linsert(1, '#'));
  974. X}
  975. X
  976. X/*
  977. X * Delete blank lines around dot. What this command does depends if dot is
  978. X * sitting on a blank line. If dot is sitting on a blank line, this command
  979. X * deletes all the blank lines above and below the current line. If it is
  980. X * sitting on a non blank line then it deletes all of the blank lines after
  981. X * the line. Normally this command is bound to "C-X C-O". Any argument is
  982. X * ignored.
  983. X */
  984. Xdeblank(f, n)
  985. X{
  986. X        register LINE   *lp1;
  987. X        register LINE   *lp2;
  988. X        long nld;
  989. X
  990. X    if (curbp->b_mode&MDVIEW)    /* don't allow this command if    */
  991. X        return(rdonly());    /* we are in read only mode    */
  992. X        lp1 = curwp->w_dotp;
  993. X        while (llength(lp1)==0 && (lp2=lback(lp1))!=curbp->b_linep)
  994. X                lp1 = lp2;
  995. X        lp2 = lp1;
  996. X        nld = 0;
  997. X        while ((lp2=lforw(lp2))!=curbp->b_linep && llength(lp2)==0)
  998. X                ++nld;
  999. X        if (nld == 0)
  1000. X                return (TRUE);
  1001. X        curwp->w_dotp = lforw(lp1);
  1002. X        curwp->w_doto = 0;
  1003. X        return (ldelete(nld, FALSE));
  1004. X}
  1005. X
  1006. X/*
  1007. X * Insert a newline, then enough tabs and spaces to duplicate the indentation
  1008. X * of the previous line. Assumes tabs are every eight characters. Quite simple.
  1009. X * Figure out the indentation of the current line. Insert a newline by calling
  1010. X * the standard routine. Insert the indentation by inserting the right number
  1011. X * of tabs and spaces. Return TRUE if all ok. Return FALSE if one of the
  1012. X * subcomands failed. Normally bound to "C-J".
  1013. X */
  1014. Xindent(f, n)
  1015. X{
  1016. X        register int    nicol;
  1017. X        register int    c;
  1018. X        register int    i;
  1019. X
  1020. X    if (curbp->b_mode&MDVIEW)    /* don't allow this command if    */
  1021. X        return(rdonly());    /* we are in read only mode    */
  1022. X        if (n < 0)
  1023. X                return (FALSE);
  1024. X        while (n--) {
  1025. X                nicol = 0;
  1026. X                for (i=0; i<llength(curwp->w_dotp); ++i) {
  1027. X                        c = lgetc(curwp->w_dotp, i);
  1028. X                        if (c!=' ' && c!='\t')
  1029. X                                break;
  1030. X                        if (c == '\t')
  1031. X                                nicol |= 0x07;
  1032. X                        ++nicol;
  1033. X                }
  1034. X                if (lnewline() == FALSE
  1035. X                || ((i=nicol/8)!=0 && linsert(i, '\t')==FALSE)
  1036. X                || ((i=nicol%8)!=0 && linsert(i,  ' ')==FALSE))
  1037. X                        return (FALSE);
  1038. X        }
  1039. X        return (TRUE);
  1040. X}
  1041. X
  1042. X/*
  1043. X * Delete forward. This is real easy, because the basic delete routine does
  1044. X * all of the work. Watches for negative arguments, and does the right thing.
  1045. X * If any argument is present, it kills rather than deletes, to prevent loss
  1046. X * of text if typed with a big argument. Normally bound to "C-D".
  1047. X */
  1048. Xforwdel(f, n)
  1049. X{
  1050. X    if (curbp->b_mode&MDVIEW)    /* don't allow this command if    */
  1051. X        return(rdonly());    /* we are in read only mode    */
  1052. X        if (n < 0)
  1053. X                return (backdel(f, -n));
  1054. X        if (f != FALSE) {                       /* Really a kill.       */
  1055. X                if ((lastflag&CFKILL) == 0)
  1056. X                        kdelete();
  1057. X                thisflag |= CFKILL;
  1058. X        }
  1059. X        return (ldelete((long)n, f));
  1060. X}
  1061. X
  1062. X/*
  1063. X * Delete backwards. This is quite easy too, because it's all done with other
  1064. X * functions. Just move the cursor back, and delete forwards. Like delete
  1065. X * forward, this actually does a kill if presented with an argument. Bound to
  1066. X * both "RUBOUT" and "C-H".
  1067. X */
  1068. Xbackdel(f, n)
  1069. X{
  1070. X        register int    s;
  1071. X
  1072. X    if (curbp->b_mode&MDVIEW)    /* don't allow this command if    */
  1073. X        return(rdonly());    /* we are in read only mode    */
  1074. X        if (n < 0)
  1075. X                return (forwdel(f, -n));
  1076. X        if (f != FALSE) {                       /* Really a kill.       */
  1077. X                if ((lastflag&CFKILL) == 0)
  1078. X                        kdelete();
  1079. X                thisflag |= CFKILL;
  1080. X        }
  1081. X        if ((s=backchar(f, n)) == TRUE)
  1082. X                s = ldelete((long)n, f);
  1083. X        return (s);
  1084. X}
  1085. X
  1086. X/*
  1087. X * Kill text. If called without an argument, it kills from dot to the end of
  1088. X * the line, unless it is at the end of the line, when it kills the newline.
  1089. X * If called with an argument of 0, it kills from the start of the line to dot.
  1090. X * If called with a positive argument, it kills from dot forward over that
  1091. X * number of newlines. If called with a negative argument it kills backwards
  1092. X * that number of newlines. Normally bound to "C-K".
  1093. X */
  1094. Xkilltext(f, n)
  1095. X{
  1096. X        register LINE   *nextp;
  1097. X        long chunk;
  1098. X
  1099. X    if (curbp->b_mode&MDVIEW)    /* don't allow this command if    */
  1100. X        return(rdonly());    /* we are in read only mode    */
  1101. X        if ((lastflag&CFKILL) == 0)             /* Clear kill buffer if */
  1102. X                kdelete();                      /* last wasn't a kill.  */
  1103. X        thisflag |= CFKILL;
  1104. X        if (f == FALSE) {
  1105. X                chunk = llength(curwp->w_dotp)-curwp->w_doto;
  1106. X                if (chunk == 0)
  1107. X                        chunk = 1;
  1108. X        } else if (n == 0) {
  1109. X                chunk = curwp->w_doto;
  1110. X                curwp->w_doto = 0;
  1111. X        } else if (n > 0) {
  1112. X                chunk = llength(curwp->w_dotp)-curwp->w_doto+1;
  1113. X                nextp = lforw(curwp->w_dotp);
  1114. X                while (--n) {
  1115. X                        if (nextp == curbp->b_linep)
  1116. X                                return (FALSE);
  1117. X                        chunk += llength(nextp)+1;
  1118. X                        nextp = lforw(nextp);
  1119. X                }
  1120. X        } else {
  1121. X                mlwrite("neg kill");
  1122. X                return (FALSE);
  1123. X        }
  1124. X        return(ldelete(chunk, TRUE));
  1125. X}
  1126. X
  1127. Xsetmode(f, n)    /* prompt and set an editor mode */
  1128. X
  1129. Xint f, n;    /* default and argument */
  1130. X
  1131. X{
  1132. X    adjustmode(TRUE, FALSE);
  1133. X}
  1134. X
  1135. Xdelmode(f, n)    /* prompt and delete an editor mode */
  1136. X
  1137. Xint f, n;    /* default and argument */
  1138. X
  1139. X{
  1140. X    adjustmode(FALSE, FALSE);
  1141. X}
  1142. X
  1143. Xsetgmode(f, n)    /* prompt and set a global editor mode */
  1144. X
  1145. Xint f, n;    /* default and argument */
  1146. X
  1147. X{
  1148. X    adjustmode(TRUE, TRUE);
  1149. X}
  1150. X
  1151. Xdelgmode(f, n)    /* prompt and delete a global editor mode */
  1152. X
  1153. Xint f, n;    /* default and argument */
  1154. X
  1155. X{
  1156. X    adjustmode(FALSE, TRUE);
  1157. X}
  1158. X
  1159. Xadjustmode(kind, global)    /* change the editor mode status */
  1160. X
  1161. Xint kind;    /* true = set,        false = delete */
  1162. Xint global;    /* true = global flag,    false = current buffer flag */
  1163. X{
  1164. X    register char *scan;        /* scanning pointer to convert prompt */
  1165. X    register int i;            /* loop index */
  1166. X#if    COLOR
  1167. X    register int uflag;        /* was modename uppercase?    */
  1168. X#endif
  1169. X    char prompt[50];    /* string to prompt user with */
  1170. X    char cbuf[NPAT];        /* buffer to recieve mode name into */
  1171. X
  1172. X    /* build the proper prompt string */
  1173. X    if (global)
  1174. X        strcpy(prompt,"Global mode to ");
  1175. X    else
  1176. X        strcpy(prompt,"Mode to ");
  1177. X
  1178. X    if (kind == TRUE)
  1179. X        strcat(prompt, "add: ");
  1180. X    else
  1181. X        strcat(prompt, "delete: ");
  1182. X
  1183. X    /* prompt the user and get an answer */
  1184. X
  1185. X    mlreply(prompt, cbuf, NPAT - 1);
  1186. X
  1187. X    /* make it uppercase */
  1188. X
  1189. X    scan = cbuf;
  1190. X#if    COLOR
  1191. X    uflag = (*scan >= 'A' && *scan <= 'Z');
  1192. X#endif
  1193. X    while (*scan != 0) {
  1194. X        if (*scan >= 'a' && *scan <= 'z')
  1195. X            *scan = *scan - 32;
  1196. X        scan++;
  1197. X    }
  1198. X
  1199. X    /* test it first against the colors we know */
  1200. X    for (i=0; i<NCOLORS; i++) {
  1201. X        if (strcmp(cbuf, cname[i]) == 0) {
  1202. X            /* finding the match, we set the color */
  1203. X#if    COLOR
  1204. X            if (uflag)
  1205. X                if (global)
  1206. X                    gfcolor = i;
  1207. X                else
  1208. X                    curwp->w_fcolor = i;
  1209. X            else
  1210. X                if (global)
  1211. X                    gbcolor = i;
  1212. X                else
  1213. X                    curwp->w_bcolor = i;
  1214. X
  1215. X            curwp->w_flag |= WFCOLR;
  1216. X#endif
  1217. X            mlerase();
  1218. X            return(TRUE);
  1219. X        }
  1220. X    }
  1221. X
  1222. X    /* test it against the modes we know */
  1223. X
  1224. X    for (i=0; i < NUMMODES; i++) {
  1225. X        if (strcmp(cbuf, modename[i]) == 0) {
  1226. X            /* finding a match, we process it */
  1227. X            if (kind == TRUE)
  1228. X                if (global)
  1229. X                    gmode |= (1 << i);
  1230. X                else
  1231. X                    curwp->w_bufp->b_mode |= (1 << i);
  1232. X            else
  1233. X                if (global)
  1234. X                    gmode &= ~(1 << i);
  1235. X                else
  1236. X                    curwp->w_bufp->b_mode &= ~(1 << i);
  1237. X            /* display new mode line */
  1238. X            if (global == 0)
  1239. X                upmode();
  1240. X            mlerase();    /* erase the junk */
  1241. X            return(TRUE);
  1242. X        }
  1243. X    }
  1244. X
  1245. X    mlwrite("No such mode!");
  1246. X    return(FALSE);
  1247. X}
  1248. X
  1249. X/*    This function simply clears the message line,
  1250. X        mainly for macro usage            */
  1251. X
  1252. Xclrmes(f, n)
  1253. X
  1254. Xint f, n;    /* arguments ignored */
  1255. X
  1256. X{
  1257. X    mlwrite("");
  1258. X    return(TRUE);
  1259. X}
  1260. X
  1261. X/*    This function writes a string on the message line
  1262. X        mainly for macro usage            */
  1263. X
  1264. Xwritemsg(f, n)
  1265. X
  1266. Xint f, n;    /* arguments ignored */
  1267. X
  1268. X{
  1269. X    register char *sp;    /* pointer into buf to expand %s */
  1270. X    register char *np;    /* ptr into nbuf */
  1271. X    register int status;
  1272. X    char buf[NPAT];        /* buffer to recieve message into */
  1273. X    char nbuf[NPAT*2];    /* buffer to expand string into */
  1274. X
  1275. X    if ((status = mlreply("Message to write: ", buf, NPAT - 1)) != TRUE)
  1276. X        return(status);
  1277. X
  1278. X    /* expand all '%' to "%%" so mlwrite won't expect arguments */
  1279. X    sp = buf;
  1280. X    np = nbuf;
  1281. X    while (*sp) {
  1282. X        *np++ = *sp;
  1283. X        if (*sp++ == '%')
  1284. X            *np++ = '%';
  1285. X    }
  1286. X    *np = '\0';
  1287. X    mlwrite(nbuf);
  1288. X    return(TRUE);
  1289. X}
  1290. X
  1291. X#if    CFENCE
  1292. X/*    the cursor is moved to a matching fence    */
  1293. X
  1294. Xgetfence(f, n)
  1295. X
  1296. Xint f, n;    /* not used */
  1297. X
  1298. X{
  1299. X    register LINE *oldlp;    /* original line pointer */
  1300. X    register int oldoff;    /* and offset */
  1301. X    register int sdir;    /* direction of search (1/-1) */
  1302. X    register int count;    /* current fence level count */
  1303. X    register char ch;    /* fence type to match against */
  1304. X    register char ofence;    /* open fence */
  1305. X    register char c;    /* current character in scan */
  1306. X
  1307. X    /* save the original cursor position */
  1308. X    oldlp = curwp->w_dotp;
  1309. X    oldoff = curwp->w_doto;
  1310. X
  1311. X    /* get the current character */
  1312. X    ch = lgetc(oldlp, oldoff);
  1313. X
  1314. X    /* setup proper matching fence */
  1315. X    switch (ch) {
  1316. X        case '(': ofence = ')'; sdir = FORWARD; break;
  1317. X        case '{': ofence = '}'; sdir = FORWARD; break;
  1318. X        case '[': ofence = ']'; sdir = FORWARD; break;
  1319. X        case ')': ofence = '('; sdir = REVERSE; break;
  1320. X        case '}': ofence = '{'; sdir = REVERSE; break;
  1321. X        case ']': ofence = '['; sdir = REVERSE; break;
  1322. X        default: TTbeep(); return(FALSE);
  1323. X    }
  1324. X
  1325. X    /* set up for scan */
  1326. X    count = 1;
  1327. X    if (sdir == REVERSE)
  1328. X        backchar(FALSE, 1);
  1329. X    else
  1330. X        forwchar(FALSE, 1);
  1331. X
  1332. X    /* scan until we find it, or reach the end of file */
  1333. X    while (count > 0) {
  1334. X        c = lgetc(curwp->w_dotp, curwp->w_doto);
  1335. X        if (c == ch)
  1336. X            ++count;
  1337. X        if (c == ofence)
  1338. X            --count;
  1339. X        if (sdir == FORWARD)
  1340. X            forwchar(FALSE, 1);
  1341. X        else
  1342. X            backchar(FALSE, 1);
  1343. X        if (boundry(curwp->w_dotp, curwp->w_doto, sdir))
  1344. X            break;
  1345. X    }
  1346. X
  1347. X    /* if count is zero, we have a match, move the sucker */
  1348. X    if (count == 0) {
  1349. X        if (sdir == FORWARD)
  1350. X            backchar(FALSE, 1);
  1351. X        else
  1352. X            forwchar(FALSE, 1);
  1353. X        curwp->w_flag |= WFMOVE;
  1354. X        return(TRUE);
  1355. X    }
  1356. X
  1357. X    /* restore the current position */
  1358. X    curwp->w_dotp = oldlp;
  1359. X    curwp->w_doto = oldoff;
  1360. X    TTbeep();
  1361. X    return(FALSE);
  1362. X}
  1363. X#endif
  1364. X
  1365. X/*    Close fences are matched against their partners, and if
  1366. X    on screen the cursor briefly lights there        */
  1367. X
  1368. Xfmatch(ch)
  1369. X
  1370. Xchar ch;    /* fence type to match against */
  1371. X
  1372. X{
  1373. X    register LINE *oldlp;    /* original line pointer */
  1374. X    register int oldoff;    /* and offset */
  1375. X    register LINE *toplp;    /* top line in current window */
  1376. X    register int count;    /* current fence level count */
  1377. X    register char opench;    /* open fence */
  1378. X    register char c;    /* current character in scan */
  1379. X    register int i;
  1380. X
  1381. X    /* first get the display update out there */
  1382. X    update(FALSE);
  1383. X
  1384. X    /* save the original cursor position */
  1385. X    oldlp = curwp->w_dotp;
  1386. X    oldoff = curwp->w_doto;
  1387. X
  1388. X    /* setup proper open fence for passed close fence */
  1389. X    if (ch == ')')
  1390. X        opench = '(';
  1391. X    else if (ch == '}')
  1392. X        opench = '{';
  1393. X    else
  1394. X        opench = '[';
  1395. X
  1396. X    /* find the top line and set up for scan */
  1397. X    toplp = curwp->w_linep->l_bp;
  1398. X    count = 1;
  1399. X    backchar(FALSE, 2);
  1400. X
  1401. X    /* scan back until we find it, or reach past the top of the window */
  1402. X    while (count > 0 && curwp->w_dotp != toplp) {
  1403. X        c = lgetc(curwp->w_dotp, curwp->w_doto);
  1404. X        if (c == ch)
  1405. X            ++count;
  1406. X        if (c == opench)
  1407. X            --count;
  1408. X        backchar(FALSE, 1);
  1409. X        if (curwp->w_dotp == curwp->w_bufp->b_linep->l_fp &&
  1410. X            curwp->w_doto == 0)
  1411. X            break;
  1412. X    }
  1413. X
  1414. X    /* if count is zero, we have a match, display the sucker */
  1415. X    /* there is a real machine dependant timing problem here we have
  1416. X       yet to solve......... */
  1417. X    if (count == 0) {
  1418. X        forwchar(FALSE, 1);
  1419. X        for (i = 0; i < term.t_pause; i++)
  1420. X            update(FALSE);
  1421. X    }
  1422. X
  1423. X    /* restore the current position */
  1424. X    curwp->w_dotp = oldlp;
  1425. X    curwp->w_doto = oldoff;
  1426. X    return(TRUE);
  1427. X}
  1428. X
  1429. Xistring(f, n)    /* ask for and insert a string into the current
  1430. X           buffer at the current point */
  1431. X
  1432. Xint f, n;    /* ignored arguments */
  1433. X
  1434. X{
  1435. X    register char *tp;    /* pointer into string to add */
  1436. X    register int status;    /* status return code */
  1437. X    char tstring[NPAT+1];    /* string to add */
  1438. X
  1439. X    /* ask for string to insert */
  1440. X    status = mlreplyt("String to insert<META>: ", tstring, NPAT, metac);
  1441. X    if (status != TRUE)
  1442. X        return(status);
  1443. X
  1444. X    if (f == FALSE)
  1445. X        n = 1;
  1446. X
  1447. X    if (n < 0)
  1448. X        n = - n;
  1449. X
  1450. X    /* insert it */
  1451. X    while (n--) {
  1452. X        tp = &tstring[0];
  1453. X        while (*tp) {
  1454. X            if (*tp == 0x0a)
  1455. X                status = lnewline();
  1456. X            else
  1457. X                status = linsert(1, *tp);
  1458. X            ++tp;
  1459. X            if (status != TRUE)
  1460. X                return(status);
  1461. X        }
  1462. X    }
  1463. X        
  1464. X    return(TRUE);
  1465. X}
  1466. X
  1467. END_OF_random.c
  1468. if test 23797 -ne `wc -c <random.c`; then
  1469.     echo shar: \"random.c\" unpacked with wrong size!
  1470. fi
  1471. # end of overwriting check
  1472. fi
  1473. echo shar: End of archive 10 \(of 14\).
  1474. cp /dev/null ark10isdone
  1475. MISSING=""
  1476. for I in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ; do
  1477.     if test ! -f ark${I}isdone ; then
  1478.     MISSING="${MISSING} ${I}"
  1479.     fi
  1480. done
  1481. if test "${MISSING}" = "" ; then
  1482.     echo You have unpacked all 14 archives.
  1483.     echo "See the readme file"
  1484.     rm -f ark[1-9]isdone ark[1-9][0-9]isdone
  1485. else
  1486.     echo You still need to unpack the following archives:
  1487.     echo "        " ${MISSING}
  1488. fi
  1489. ##  End of shell archive.
  1490. exit 0
  1491.